#include "Renderer/TitlebarRenderer.h"
#include "Scheme/GUIWidgetLookManager.h"
#include "GUIRenderer.h"
#include "GUISystem.h"

namespace YGUI
{
    const String TitlebarRenderer::TypeName("Titlebar");

    TitlebarRenderer::TitlebarRenderer(const String& type) : WindowRenderer(type)
    {
    }

    void TitlebarRenderer::render(void)
    {
        const StateImagery* imagery = NULL;;
        try
        {
            const WidgetLook& look = getLook();
            if(m_window->isDisabled())
                imagery = &look.getStateImagery("Disabled");
            else 
            {
                const Window* parent = m_window->getParent();
                String state = (parent && parent->isActive()) ? "Active" : "Inactive";
                imagery = &look.getStateImagery(state);
            }
            imagery->render(*m_window);
        }
        catch(...)
        {
        }
    }

    Rect<float> TitlebarRenderer::getScreenRect(void) const
    {
        const Window* parent = m_window->getParent();
        const Rect<float>& rect = m_window->getUnclippedScreenRect();
        if(parent && parent->getParent() && m_window->isClippedByParent())
        {
            return parent->getParent()->getClientRect().getIntersection(rect);
        }
        else
        {
            return Rect<float>(Point<float>(), System::getSingleton().getRenderer()->getSize());
        }
    }
}
